function z = respfctAUG2(x)
% function z = respfct(x)
%    this function evaluates the nonlinear residual
%    for the current balance and keff estimate
% this is the augmented nonlinear function
% which includes lambda as a variable.
% Note, x is now:
%    x(1:end-2) = currents
%    x(1:end-1) = keff
%    x(end)     = lambda

% declare the global problem variables
global ne dc sa ns sw bcL bcR gm



% compute the response matrices
M = connect(ne,bcL,bcR);
[R F A L M] = respmtx(ne, dc, sa, ns, sw, x(1:end-1) );


% compute the nonlinear function, FF(x)*x = 0
FF = [ R*M-x(end)*eye(ne*2)  zeros(ne*2,1) 
       F*M                  -sum(A*M*x(1:end-2))-sum(L)    ];  
z1 = FF*(x(1:end-1)); %
z2 = 0.5 - 0.5*sum(x(1:end-2));
%z2 = 0.5 - 0.5*x(1:end-2)'*x(1:end-2);

z  = [z1' z2']';

   

